//
//  NetworkTools.swift
//  SinaWeibo
//
//  Created by mac on 15/12/29.
//  Copyright © 2015年 mac. All rights reserved.
//

import AFNetworking
let dataErrorDomain = "com.baidu.data.error"

//枚举任意类型
enum HTTPMethod : String {
    case GET = "GET"
    case POST = "POST"
}

//网络请求处理层  以后的网络请求  都使用这个类
class NetworkTools: AFHTTPSessionManager {
    //创建单利例对象 
    static let sharedTools : NetworkTools =  {
        let urlString = "https://api.weibo.com/"
        let url = NSURL(string: urlString)
        let tools = NetworkTools(baseURL: url)
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    //网络请求的核心方法封装
    func requestJSONDict(method : HTTPMethod ,urlString : String ,parameters:[String:String]?,finished:(dict:[String : AnyObject]? ,error:NSError?) ->()) {
        if method == HTTPMethod.POST{
        //发送 POST 请求 
        
    POST(urlString, parameters: parameters, progress: { (p) -> Void in
        print(p)
        }, success: { (_, result) -> Void in
            //请求的成功的回调
           // print("wetertr")
            print(result)
            guard let dict = result as? [String : AnyObject] else {
                //不能够转换为字典数据
                //执行失败的回调
                //domain: 反转的域名  com.baidu.error
                //code : 错误状态码 自定义的错误信息 一般使用 负数
                let myError = NSError(domain:dataErrorDomain , code: -10000, userInfo: [NSLocalizedDescriptionKey : "数据不合法"])
                print(myError)
                finished(dict: nil, error: myError)
                return
            }
            
            //执行成功的回调
            finished(dict: dict, error: nil)
            
        }) { (_, error) -> Void in
            //执行失败的回调
            finished(dict: nil, error: error)
            //请求失败的回调
            print(error)
        }
        
        }
        
        if method == HTTPMethod.GET {
        GET(urlString, parameters: parameters, progress: { (p) -> Void in
            print(p)
            }, success: { (_, result) -> Void in
                //请求的成功的回调
                //                    print(result)
                guard let dict = result as? [String : AnyObject] else {
                    //不能够转换为字典数据
                    //执行失败的回调
                    //domain: 反转的域名  com.baidu.error
                    //code : 错误状态码 自定义的错误信息 一般使用 负数
                    let myError = NSError(domain:dataErrorDomain , code: -10000, userInfo: [NSLocalizedDescriptionKey : "数据不合法"])
                    print(myError)
                    finished(dict: nil, error: myError)
                    return
                }
                
                //执行成功的回调
                finished(dict: dict, error: nil)
            }) { (_, error) -> Void in
                //执行失败的回调
                finished(dict: nil, error: error)
                //请求失败的回调
                print(error)
        }
        }
    }
    
    
    //封装 get 请求
    /*
    AFN.GET(urlString, parameters: parameters, progress: { (p) -> Void in
    // print(p)
    }, success: { (task, result) -> Void in
    //需要判断result 能否转化为字典
    guard let dict = result as? [String : AnyObject] else {
    print("数据不合法")
    SVProgressHUD.showInfoWithStatus("你得网络出错请稍后再试")
    return
    }
    if let array = dict["statuses"] as? [[String : AnyObject]] {
    //  print("获取 statuses对应的数组\(array)结束")
    //TODO: 遍历数组中 所有的字典 做字典转模型的操作
    var list = [Status]()
    for item in array{
    let s = Status(dict: item)
    list.append(s)
    // print(list)
    
    
    }
    self.statuses = list
    //刷新表视图
    self.tableView.reloadData()
    
    }
    
    }) { (task, error) -> Void in
    //  print(error)
    }
    }

    */
    
   

    
    
}
